#include<iostream>
#include"Socket.hpp"
#include"Protocol.hpp"
#include<memory>
using namespace SocketMoudle;
void getnum(int *x,int *y,char * oper){
    //从标准输入中获取数据
        std::cout<<"please Enter x"<<std::endl;
        std::cin>>*x;
        std::cout<<"please Enter y"<<std::endl;
        std::cin>>*y;
        std::cout<<"please Enter oper"<<std::endl;
        std::cin>>*oper;

}
int main(int argc, char* argv[]){
    if(argc!=3){
        std::cout<<"Usage: "<<argv[0]<<" <server_ip> <server_port>"<<std::endl;
        return 1;
    }
    std::string server_ip=argv[1];
    uint16_t server_port=std::stoi(argv[2]);
    std::shared_ptr<Socket> sock=std::make_shared<TcpSocket>(server_ip,server_port);
    if(sock->Connect(server_ip,server_port)==0){
        //成功
        LOG(LogLevel::INFO)<<"Connect to server success";

    }
    else {
        LOG(LogLevel::ERROR)<<"Connect to server fail";
        exit(1);
    }
    std::unique_ptr<Protocol>protocol=std::make_unique<Protocol>();

    while(true){
        int x,y;
        char oper;
        getnum(&x,&y,&oper);
        //组织请求 可以直接发送的字符串
        std::string req_string=protocol->BuildRequeststring(x,y,oper);
        //发送请求
        sock->Send(req_string);
        //获取应答
        std::string buffer;
        Response response;
        int ret=protocol->GetResponse(sock,buffer,response);
        if(ret==false){
            LOG(LogLevel::ERROR)<<"GetResponse failed";
            break;
        }
        //打印结果
        std::cout<<"Result:"<<response.getResult()<<std::endl;
        std::cout<<"code:"<<response.getCode()<<std::endl;
    }
    sock->Close();
    return 0;
}